package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.Goods;
import light.head.model.Stock;
/***
 * 物品库存信息
 * @author yy
 *
 */
@Service
public class StockDAO extends BaseDAO<Stock, Integer>{
	/***
	 * 查询
	 */
	public RESULT fingByMap(Map<String, String> map , PageBean pb){
		StringBuffer hql = new StringBuffer("FROM Stock s WHERE 1=1 ");
		List<Object> list = null;
		if(map != null){
			list = new ArrayList<Object>();
			String name = map.get(Parm.NAME);
			String sort = map.get(Parm.SORT);
			String type = map.get(Parm.TYPE);
			if (isNotNull(name)) {
				hql.append(" AND s.gn = (SELECT g.name FROM Goods g WHERE g.code = ?)");
				list.add("%" + name + "%");
			}if (isNotNull(sort)) {
				hql.append(" AND s.sort = ?");
				list.add(Byte.parseByte(sort));
			}if (isNotNull(type)) {
				hql.append(" AND s.type = ?");
				list.add(Integer.parseInt(type));
			}
		}	 
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
  
	/**
	 * 根据条件分页查询商品信息
	 * @param	查询条件
	 * @param	分页对象
	 */
	public RESULT findGoods(Map<String, String> map, PageBean pb) {
		StringBuffer hql = new StringBuffer("FROM Goods g WHERE g.deleted <> 1 AND g.deleted IS NOT NULL");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();

			String name = map.get(Parm.NAME);
			String eName = map.get(Parm.ENAME);
			String oc = map.get(Parm.OC);

			if (isNotNull(name)) {
				hql.append(" AND g.name LIKE ?");
				list.add("%" + name + "%");
			}
			if (isNotNull(oc)) {
				hql.append(" AND g.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			if (isNotNull(eName)) {
				hql.append(" AND g.eName LIKE ?");
				list.add("%" + eName + "%");
			}
		}
		hql.append(" ORDER BY g.oc, g.code DESC");
		
		return findToPageBean(hql.toString(), pb, list);
	}
  /**
	 * 根据拼接好的用户编号序列获取对应的用户列表
	 */
	public List<Goods> findGoodsByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM Goods g WHERE g.deleted <> 1 AND g.deleted IS NOT NULL AND g.code IN (" + codes + ")";
		List<Goods> list = find(sql);
		return list;
	}
  
	
	
	public RESULT findByMapAndOcs(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if (null == ocs || ocs.size() == 0) return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer(" FROM Stock s WHERE s.oc IN (");
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String name = map.get(Parm.NAME);
			String sort = map.get(Parm.SORT);
			String deleted = map.get(Parm.DELETED);
			
			if(isNotNull(name)){
				hql.append(" AND s.gn LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(sort)){
				hql.append(" AND s.sort = ?");
				list.add(Byte.parseByte(sort));
			}if (isNotNull(deleted)) {
				hql.append(" AND s.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
//	/**
//	 * 根据商品编号获取当前机构的库存情况
//	 */
//	public List<Stock> findByGC(List<Integer> ocs, String[] gc){
//		if(ocs == null || gc.length == 1) return null;
//		
//		StringBuffer sql = new StringBuffer("FROM Stock s WHERE s.oc = ?");
//		List<Object> parm = new ArrayList<Object>();
//		parm.add(ocs);
//		
//		sql.append(" AND s.gc IN (");
//		for(int i = 1 ; i < gc.length ;i ++){
//			if(gc[i] != null && !gc[i].equals("")){
//				sql.append("?,");
//				parm.add(Integer.parseInt(gc[i]));
//			}
//		}
//		
//		sql.deleteCharAt(sql.length() - 1).append(")");
//		List<Stock> list = find(sql.toString(), null, null, parm.toArray());
//		return list;
//	}
	
	/**
	 * 根据商品编号获取当前机构的库存情况
	 */
	public List<Stock> findByGC(Integer gc, List<Integer> ocs){
		if(null == gc || ocs == null || ocs.size() == 0 ) return null;
		
		StringBuffer hql = new StringBuffer("FROM Stock s WHERE s.gc = ? AND s.oc IN (");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(gc);
		for (Integer i : ocs) {
			if (null != i) {
				hql.append("?,");
				parm.add(i);
			}
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), parm.toArray());
	}
	
	
	
	public RESULT update(Stock s){
		if (null == s || s.getCode() == null)
			return RESULT.PARAMETER_ERROR;
		
		Stock old = get(s.getCode());
		if (null == old)
			return RESULT.NOT_FIND;
		
		return upd(old);
	}
}
